package com.ft.springboottravel.service.impl;

import com.ft.springboottravel.bean.Route_Img;
import com.ft.springboottravel.mapper.Route_ImgMapper;
import com.ft.springboottravel.service.Route_ImgService;
import com.ft.springboottravel.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
public class Route_ImgServiceimpl  implements Route_ImgService {

    @Autowired
    private Route_ImgMapper Route_ImgMapper;


    @Override
    public Pager<Route_Img> selectlistRoute_Img(Integer Route_Id, Integer currentPage) {

        currentPage = currentPage == null ? 1 : currentPage > 0 ? currentPage:1 ;


        System.out.println(currentPage);
        List<Route_Img> route_imgs = Route_ImgMapper.selectlistRoute_Img(1, (currentPage - 1) * 4);
        for (Route_Img route_img : route_imgs) {
            System.out.println(route_img.toString());
        }

        Pager<Route_Img> objectPager = new Pager<>();
        objectPager.setList(route_imgs);
        objectPager.setCurrentPage(currentPage);
        objectPager.setPageSize(4);
        objectPager.setTotal(selectLong(Route_Id));
        return objectPager;
    }

    @Override
    public long selectLong(Integer Route_Id) {
        return  Route_ImgMapper.selectLong(Route_Id);
    }

    @Override
    public Route_Img selectRoute_Img(Integer img_id) {
        return Route_ImgMapper.selectRoute_Img(img_id);
    }

    @Override
    public void addRoute_Img(Route_Img Route_Img, MultipartFile img) {
        String addimg = addimg(img);
        System.out.println(addimg);
        if (addimg.equals("")){
            return;
        }
        Route_Img.setImgSrc(addimg);
        Route_ImgMapper.addRoute_Img(Route_Img);
    }


    @Override
    public void dele(Integer Route_Img_Id) {
        boolean delete = new File("D:/updates/" + selectRoute_Img(Route_Img_Id).getImgSrc()).delete();
        Route_ImgMapper.dele(Route_Img_Id);
    }

    @Override
    public void edit(MultipartFile img,Route_Img Route_Img) {
        String editimg = editimg(img, Route_Img.getImgId());
        Route_Img.setImgSrc(editimg);
        Route_ImgMapper.edit(Route_Img);
    }

    /**
     * 修改图片
     * @param img
     * @param Img_Id 要修改的img id
     * @return
     */
    public String editimg(MultipartFile img,Integer Img_Id){
        Route_Img route_img = selectRoute_Img(Img_Id);
        String originalFilename = img.getOriginalFilename();
        if (originalFilename.length()>0){
            System.out.println("chuan");
            File File = new File( "D:/updates/" + route_img.getImgSrc());
            File.delete();
            String addimg = addimg(img);
            return addimg;
        }else {
            System.out.println("!chuan");
            return route_img.getImgSrc();
        }
    }


    /**
     *  添加图片到本地
     * @param img
     * @return
     */
    public String addimg(MultipartFile img){
        String originalFilename = img.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String imgname = UUID.randomUUID().toString().replaceAll("-", "") + substring;
        File file = new File("D:/updates/Route_Img/" + imgname) ;
        try {
            boolean mkdirs = file.mkdirs();
            System.out.println(mkdirs);
            img.transferTo(file);
            return "Route_Img/"+imgname;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }
    }



}
